package com.wing.qualityTesting.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.enums.Idempotent;
import com.wing.qualityTesting.model.response.ProcessQualityCategoryVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.qualityTesting.model.entity.ProcessQualityCategory;
import com.wing.qualityTesting.model.request.ProcessQualityCategoryForm;
import com.wing.qualityTesting.service.ProcessQualityCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-质量参数分类管理", tags = "#/qualityTesting/processQualityCategory")
@RestController("processQualityCategoryController")
@RequestMapping("qualityTesting/admin/processQualityCategory")
@RequiredArgsConstructor
public class ProcessQualityCategoryController {

    private final ProcessQualityCategoryService processQualityCategoryService;

    @ApiOperation(value = "查询所有质量参数分类", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProcessQualityCategoryVO> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,Long productTypeId
    ){

        Page<ProcessQualityCategory> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort",StrUtil.toUnderlineCase(sort));
        paramMap.put("order",StrUtil.toUnderlineCase(order));
        if(productTypeId != null){
            paramMap.put("productType",productTypeId);
        }

        IPage<ProcessQualityCategoryVO> list = processQualityCategoryService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @ApiOperation(value = "添加质量参数分类", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    @Idempotent(keyPrefix = "processQualityCategoryAdd")
    public JsonResult add(@Validated(ProcessQualityCategoryForm.Add.class) ProcessQualityCategoryForm processQualityCategoryForm) {
        if (processQualityCategoryService.add(processQualityCategoryForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }




    @ApiOperation(value = "修改质量参数分类", notes = "修改")
    @PutMapping("/update")
    @Idempotent(keyPrefix = "processQualityCategoryUpdate")
    public JsonResult update(@Validated(ProcessQualityCategoryForm.Update.class) ProcessQualityCategoryForm processQualityCategoryForm) {
        ProcessQualityCategory processQualityCategory = processQualityCategoryService.getById(processQualityCategoryForm.getId());
        if(processQualityCategory == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(ProcessQualityCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(processQualityCategoryForm, processQualityCategory, copyOptions);
        if (processQualityCategoryService.updateById(processQualityCategory)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }



    @ApiOperation(value = "删除质量参数分类", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processQualityCategoryId", value = "质量参数分类id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long processQualityCategoryId) {
        if (processQualityCategoryService.removeById(processQualityCategoryId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }


    @ApiOperation(value = "启用质量参数分类", notes = "启用")
    @com.wing.common.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/enable")
    public JsonResult enable(@Validated(ProcessQualityCategoryForm.Add.class) ProcessQualityCategoryForm processQualityCategoryForm) {

        if (processQualityCategoryService.enable(processQualityCategoryForm.getId())>=0) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用质量参数分类", notes = "停用")
    @com.wing.common.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/disable")
    public JsonResult disable(@Validated(ProcessQualityCategoryForm.Add.class) ProcessQualityCategoryForm processQualityCategoryForm) {

        if (processQualityCategoryService.disable(processQualityCategoryForm.getId())>=0) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }

    @ApiOperation(value = "获取所有质量参数分类树", notes = "搜索")
    @com.wing.common.annotation.RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @GetMapping("/getTreeData")
    public JsonResult getTreeData() {

        return processQualityCategoryService.getTreeData();
    }
}